<?php
namespace App\Service\Volidate;

/**
 * @author chenshubo
 * 创建订单请求参数校验器
 */
use App\Service\Code\Normal;

class OrderCreateVolidate 
{
    //限制下单数量
    CONST ORDER_SIZE = 50 ;
    
    public $limit_user = [
        'jd' => '京东商户'
    ];
    
    //敏感信息用户
    public static $sensitive_user = [
        'pdd'   => '拼多多',
        'jd'    => '京东'
    ];
    
    //需要下游发起重新推送操作渠道商
    public static $retry_pay_user = [
        'pdd'   => '拼多多',
        'jd'    => '京东'
    ];
    
    /**
     * 校验输入参数
     * @param array $param
     */
    public static function checkParametersByRequest( $input = [] )
    {
        $param = $input['requestData'];
        $binfo = $input['binfo'];
        if( empty($param['goods']) ) {
            return Normal::returnCode(Normal::FAIL,"goods商品组不能为空");
        }
        
        $goodsList   = $param['goods'];
        if( !is_array($goodsList) ) {
            return Normal::returnCode(Normal::FAIL,"goods不是数组对象");
        }
        
        if( count($goodsList) > self::ORDER_SIZE ) {
            return Normal::returnCode(Normal::FAIL,"商品组不能超过".(self::ORDER_SIZE)."个");
        }
        
        if( empty($param['orderSn']) ) {
            return Normal::returnCode(Normal::FAIL,"orderSn订单号不能为空");
        }
        
        if( empty($param['buyerRealName']) ) {
            return Normal::returnCode(Normal::FAIL,"buyerRealName付款人真实姓名不能为空");
        }
        
        if( empty($param['buyerPhone']) ) {
            return Normal::returnCode(Normal::FAIL,"buyerPhone付款人电话不能为空");
        }
        
        if( empty($param['receiveName']) ) {
            return Normal::returnCode(Normal::FAIL,"receiveName收货人不能为空");
        }
        
        if( empty($param['recveivePhone']) ) {
            return Normal::returnCode(Normal::FAIL,"recveivePhone收货电话人不能为空");
        }
        
        if( empty($param['receiveAddress']) ) {
            return Normal::returnCode(Normal::FAIL,"receiveAddress收货地址不能为空");
        }
        
        if( empty($param['province']) ) {
            return Normal::returnCode(Normal::FAIL,"province省份不能为空");
        }
        
        if( empty($param['city']) ) {
            return Normal::returnCode(Normal::FAIL,"city市不能为空");
        }
        
        if( !isset($param['district']) ) {
            return Normal::returnCode(Normal::FAIL,"district地级区不能为空");
        }
        
        if( !empty($param['orderRemarks']) ) {
            $param['orderRemarks'] = checkFields($param['orderRemarks']);
        }
        
        if( !empty($param['orderSource']) ) {
            $param['orderSource'] = checkFields($param['orderSource']);
        }
        
        //是否使用优先级仓库商品扣除库存逻辑
        if( !empty($param['ignorePriority']) ) {
            $param['ignorePriority'] = checkFields($param['ignorePriority']);
        }
        
        //过滤指定字段
        $orderSn = checkFields($param['orderSn']);
        
        //如果是存在铭感信息加密之后的字段，则不检查下单字段规则问题
        if( ! (isset($param['extends']['orderType']) && isset(self::$sensitive_user[strtolower($param['extends']['orderType'])]) ) ) {
            $param['buyerRealName']     = checkFields($param['buyerRealName']);
            $param['buyerPhone']        = checkFields($param['buyerPhone']);
            $param['receiveName']       = checkFields($param['receiveName']);
            $param['recveivePhone']     = checkFields($param['recveivePhone']);
            $param['receiveAddress']    = checkFields($param['receiveAddress']);
            $param['buyerIdCard']       = checkFields($param['buyerIdCard']??'');
        }
        
        $param['province']          = checkFields($param['province']);
        $param['city']              = checkFields($param['city']);
        $param['district']          = checkFields($param['district']);
        
        //是否属于敏感信息渠道订单用户
        $isSensitive = isset(self::$sensitive_user[$binfo['user_tag']]) ? true : false;
        
        //需要校验正常下单渠道用户信息情况
        if ( !$isSensitive ) {
            
            //验证订单号
            if( $orderSn != $param['orderSn'] || strlen($param['orderSn']) > 50 ) {
                return Normal::returnCode(Normal::FAIL,"orderSn订单号只能为 50位以内的数字或字母");
            }
            
            //验证下单人
            if( !checkRealName($param['buyerRealName']) ) {
                return Normal::returnCode(Normal::FAIL,"buyerRealName付款人真实姓名格式不正确");
            }
            
            //验证号码
            if( !(checkPhone($param['buyerPhone']) || checkCallPhone($param['buyerPhone'])) ) {
                return Normal::returnCode(Normal::FAIL,"buyerPhone付款人电话格式不正确");
            }
            
            //验证号码
            if( !(checkPhone($param['recveivePhone']) || checkCallPhone($param['recveivePhone'])) ) {
                return Normal::returnCode(Normal::FAIL,"收货人电话格式不正确");
            }
        }
        
        //验证商品下单商品组
        if( empty($param['goods']) || !is_array($param['goods']) ) {
            return Normal::returnCode(Normal::FAIL,"下单商品组goods不能为空");
        }
        
        //计算订单总金额
        $goodsTotalMoney = '0.00';
        foreach ($param['goods'] as $key => & $value )
        {
            if( !isset($value['goodsBatchNo']) ) {
                return Normal::returnCode(Normal::FAIL,"goodsBatchNo参数缺失");
            }
        
            if( empty($value['goodsSku']) ) {
                return Normal::returnCode(Normal::FAIL,"goodsSku商品sku不能为空");
            }
        
            if( empty($value['goodsNum']) ) {
                return Normal::returnCode(Normal::FAIL,"goodsNum购买数量不能为空");
            }
        
            if( empty($value['goodsPrice']) ) {
                return Normal::returnCode(Normal::FAIL,"goodsPrice商品价格不能为空");
            }
        
            if( empty($value['goodsSpecNum']) ) {
                return Normal::returnCode(Normal::FAIL,"goodsSpecNum商品规格数量不能为空");
            }
            
            //验证自编码
            if( !checkOnlyDC($value['goodsSku']) ) {
                //return Normal::returnCode(Normal::FAIL,"goodsSku只能为数字或者字母");
            }
            
            //验证单价
            if( !floatCheck($value['goodsPrice']) ) {
                return Normal::returnCode(Normal::FAIL,"goodsPrice商品单价格式不正确，必须0.00");
            }
            
            //验证数量
            if( !intNumberChcek($value['goodsNum']) ) {
                return Normal::returnCode(Normal::FAIL,"goodsNum商品数量必须为正整数");
            }
            
            //规格数量
            if( !intNumberChcek($value['goodsSpecNum']) ) {
                return Normal::returnCode(Normal::FAIL,"goodsSpecNum商品数量必须为正整数");
            }
            
            $goodsTotalMoney = bcadd((string)bcmul((string)$value['goodsPrice'],(string)$value['goodsNum'],2),(string)$goodsTotalMoney,2);
        }
        
        //旧下单接口需要，分摊优惠金额到商品明细
        $isOlderOrderTrade          = isset($param['orderTradeType'])   ? false                     : true ;
        //总优惠金额
        $discountMoney              = !empty($param['discountMoney'])   ? $param['discountMoney']   : '0.00';
        //运费
        $freight                    = !empty($param['freight'])         ? $param['freight']         : '0.00';
        //真实付款金额
        $realPayAmt                 = !empty($param['orderMoney'])      ? $param['orderMoney']      : '0.00';
        
        //历史订单
        if( $isOlderOrderTrade ) 
        {
            
            //校验老系统实际支付金额问题            
            $orderMoney = bcadd((string)$goodsTotalMoney,(string)$freight,2);
            $orderRealAmt = bcsub((string)$orderMoney,(string)$discountMoney,2);
            if( bccomp((string)$realPayAmt,(string)$orderRealAmt,2) != 0 ) {
                return Normal::returnCode(Normal::FAIL,"订单实际支付金额orderMoney（{$realPayAmt}）计算错误，订单金额计算公式【订单实际支付金额orderMoney（{$orderRealAmt}）= 运费freight（{$freight}）+ 抵扣前商品行金额总和 sum(goodsNum*goodsPrice)（{$goodsTotalMoney}） - 非现金抵扣金额 discountMoney （{$discountMoney}）】");
            }
            
            $has_discount_money = '0.00';
            $new_discount_money = '0.00';
            $_count_goods = count($param['goods']);
            foreach ($param['goods'] as $key => & $new_goods_item)
            {
                $goodsPrice             = $new_goods_item['goodsPrice'];
                $goodsNum               = $new_goods_item['goodsNum'];
                $goodsSingleTotalAmt    = bcmul((string)$goodsPrice,(string)$goodsNum,2);
                
                //得到销售单价占总销售金额的比例
                if( $key == $_count_goods - 1 ) 
                {
                    $_new_goods_discount_money     = bcsub((string)$discountMoney,(string)$has_discount_money,2);
                }
                else 
                {
                    $_discount_scale               = bcdiv((string)$goodsSingleTotalAmt,(string)$goodsTotalMoney,4);
                    $_new_goods_discount_money     = bcmul((string)$_discount_scale,(string)$discountMoney,2);
                    $has_discount_money            = bcadd((string)$has_discount_money,(string)$_new_goods_discount_money,2);
                }
                
                /* //如果优惠金额大于销售单价，则需要反算加回到销售单价
                $_low_single_goods_discount_money   = bcdiv((string)$_new_goods_discount_money,(string)$goodsNum,2);
                $new_single_goods_dicount_money     = bcdiv((string)$_new_goods_discount_money,(string)$goodsNum,3);
                $new_single_goods_dicount_money     = bcmul((string)$new_single_goods_dicount_money,'100',1);
                $new_single_goods_dicount_money     = bcdiv(ceil($new_single_goods_dicount_money),'100',2);
                $_new_goods_discount_money          = bcmul((string)$new_single_goods_dicount_money,(string)$goodsNum,2);
                $new_discount_money                 = bcadd((string)$new_discount_money,(string)$_new_goods_discount_money,2);
                
                //判断优惠金额是否能除清
                $_single_price_discount_diff_money  = bcsub((string)$new_single_goods_dicount_money,(string)$_low_single_goods_discount_money,2);
                //优惠单价大于销售单价
                $new_goods_item['goodsPrice']       = bcadd((string)$goodsPrice,$_single_price_discount_diff_money,2); */
                
                $new_discount_money                 = bcadd((string)$new_discount_money,(string)$_new_goods_discount_money,2);
                //衔接新版本接口
                $new_goods_item['goodsSingleDiscountMoney']              = $_new_goods_discount_money;
                $new_goods_item['goodsSingleDiscountPointMoney']         = '0.00';
                $new_goods_item['goodsSingleDiscountRedEnvelopeMoney']   = '0.00';
                $new_goods_item['goodsSingleDiscountActivityMoney']      = '0.00';
                $new_goods_item['goodsSingleDiscountCouponMoney']        = $_new_goods_discount_money;
                $new_goods_item['goodsSingleDiscountPaywayMoney']        = '0.00';
                $new_goods_item['goodsSingleOrderPrecardBalanceMoney']   = '0.00';
                $new_goods_item['goodsSingleOrderPoints']                = '0.00';
            }
            
            $param['discountMoney']             = $new_discount_money;
            $param['discountPointMoney']        = '0.00';
            $param['discountRedEnvelopeMoney']  = '0.00';
            $param['discountRedEnvelopeName']   = '';
            $param['discountActivityMoney']     = '0.00';
            $param['discountActivityName']      = '';
            $param['discountCouponMoney']       = $new_discount_money;
            $param['discountCouponNames']       = '活动优惠抵扣金额';
            $param['discountPaywayMoney']       = '0.00';
            $param['orderPreSaleStage']         = '0';
            $param['orderPrecardBalanceMoney']  = '0.00';
            $param['orderPoints']               = '0.00';
            $discountMoney = $new_discount_money;
        }
        
        //总积分抵扣金额
        $discountPointMoney         = !empty($param['discountPointMoney']) ? $param['discountPointMoney'] : '0.00';
        //总红包抵扣金额
        $discountRedEnvelopeMoney   = !empty($param['discountRedEnvelopeMoney']) ? $param['discountRedEnvelopeMoney'] : '0.00';
        //总活动金额
        $discountActivityMoney      = !empty($param['discountActivityMoney']) ? $param['discountActivityMoney'] : '0.00';
        //总优惠券抵扣金额
        $discountCouponMoney        = !empty($param['discountCouponMoney']) ? $param['discountCouponMoney'] : '0.00';
        //总支付方式优惠金额
        $discountPaywayMoney        = !empty($param['discountPaywayMoney']) ? $param['discountPaywayMoney'] : '0.00';
        //预付卡支付金额
        $orderPrecardBalanceMoney   = !empty($param['orderPrecardBalanceMoney']) ? $param['orderPrecardBalanceMoney'] : '0.00';
        //总积分
        $orderPoints                = !empty($param['orderPoints']) ? $param['orderPoints'] : '0.00';
        
        if( bccomp((string)$freight,'0.00',2) == -1 ) {
            return Normal::returnCode(Normal::FAIL,"运费金额freight不能小于0");
        }
        
        if( bccomp((string)$discountMoney,'0.00',2) == -1 ) {
            return Normal::returnCode(Normal::FAIL,"订单优惠金额discountMoney不能小于0");
        }
        
        if( bccomp((string)$discountPointMoney,'0.00',2) == -1 ) {
            return Normal::returnCode(Normal::FAIL,"订单积分金额discountPointMoney不能小于0");
        }
        
        if( bccomp((string)$discountRedEnvelopeMoney,'0.00',2) == -1 ) {
            return Normal::returnCode(Normal::FAIL,"订单红包金额discountRedEnvelopeMoney不能小于0");
        }
        
        if( bccomp((string)$discountActivityMoney,'0.00',2) == -1 ) {
            return Normal::returnCode(Normal::FAIL,"订单活动金额discountActivityMoney不能小于0");
        }
        
        if( bccomp((string)$discountCouponMoney,'0.00',2) == -1 ) {
            return Normal::returnCode(Normal::FAIL,"订单优惠券金额discountCouponMoney不能小于0");
        }
        
        if( bccomp((string)$discountPaywayMoney,'0.00',2) == -1 ) {
            return Normal::returnCode(Normal::FAIL,"订单支付方式金额discountPaywayMoney不能小于0");
        }
		
        if( bccomp((string)$orderPrecardBalanceMoney,'0.00',2) == -1 ) {
            return Normal::returnCode(Normal::FAIL,"订单预付卡金额orderPrecardBalanceMoney不能小于0");
        }
        
        //汇总优惠金额和总优惠金额比对
        $sum_discount_money = bcadd($discountPointMoney,$discountRedEnvelopeMoney,2);
        $sum_discount_money = bcadd($sum_discount_money,$discountActivityMoney,2);
        $sum_discount_money = bcadd($sum_discount_money,$discountCouponMoney,2);
        $sum_discount_money = bcadd($sum_discount_money,$discountPaywayMoney,2);
        $sum_discount_money = bcadd($sum_discount_money,$orderPrecardBalanceMoney,2);
        
        //新版本需要校验
        if( isset($param['orderTradeType']) && bccomp($discountMoney,$sum_discount_money,2) != 0 ) {
            return Normal::returnCode(Normal::FAIL,"订单总优惠金额discountMoney[{$discountMoney}]不等于订单优惠金额明细汇总之和【discountPointMoney[{$discountPointMoney}]+discountRedEnvelopeMoney[{$discountRedEnvelopeMoney}]+discountActivityMoney[{$discountActivityMoney}]+discountCouponMoney[{$discountCouponMoney}]+discountPaywayMoney[{$discountPaywayMoney}]+orderPrecardBalanceMoney[{$orderPrecardBalanceMoney}]={$sum_discount_money}】");
        }
        
        //订单金额         
        $orderMoney = bcadd((string)$goodsTotalMoney,(string)$freight,2);
        
        //验证商品组参数是否正确
        $goodsTotalPrice                = 0 ;
        $hasFreight                     = 0;
        $hasDiscountMoney               = 0;
        $hasDiscountPonitsMoney         = 0;
        $hasDiscountRedEnvelopeMoney    = 0;
        $hasDiscountActivityMoney       = 0;
        $hasDiscountCouponMoney         = 0;
        $hasDiscountPayWayMoney         = 0;
        $hasPrecardBalanceMoney         = 0;
        $goodsTotal = count($param['goods']);
        
        //判断商品sku是否重复
        $goodsSkuList = array_column($param['goods'],'goodsSku');
        $goodsSkuRepeatArr = array_count_values($goodsSkuList);
        foreach ($goodsSkuRepeatArr as $goodsSkuKey => $count) {
            if( $count > 1 ) {
                return Normal::returnCode(Normal::FAIL,"下单商品编码【{$goodsSkuKey}】不能重复，清核对之后再进行推送订单!");
            }
        }
        
        //总优惠部分汇总计算
        $totalDiscountMoney = $totalDiscountPointMoney = $totalDiscountRedEnvelopeMoney = $totalOrderPoints = '0.00';
        $totalDiscountActivityMoney = $totalDiscountCouponMoney = $totalDiscountPaywayMoney = $totalDiscountPrecardMoney = '0.00';
        foreach ($param['goods'] as $key => & $value ) 
        {
            //商品goodsBatchNo
            $value['goodsBatchNo']  = checkFields($value['goodsBatchNo']);
            //商品Sku
            $value['goodsSku']      = checkFields($value['goodsSku']);
            //购买数量
            $value['goodsNum']      = checkFields($value['goodsNum']);
            //规格价格
            $value['goodsPrice']    = (float)checkFields($value['goodsPrice']);
            //商品税
            $goodsTax               = isset($value['goodsTax']) ? $value['goodsTax'] :  0 ;
            $value['goodsTax']      = (float)checkFields($goodsTax);
        
            $singlePrice            = bcadd($value['goodsPrice'], $value['goodsTax'],2);
            $currentGoodsTotalPrice = bcmul($value['goodsNum'], $singlePrice,2);
            $goodsTotalPrice        = bcadd($goodsTotalPrice,$currentGoodsTotalPrice ,2);
            
            //均摊运费和优惠金额
            if( $key == $goodsTotal - 1 )  {//最后一个需要减去前面已累加的数据
                $delivery_money                 = bcsub((string)$freight, $hasFreight,2);
            } else {
                //单商品占比
                $scale                          = bcdiv((string)$currentGoodsTotalPrice,(string)$orderMoney,4);
                //运费
                $delivery_money                 = bcmul((string)$freight, (string)$scale,2);
                //已累加的金额
                $hasFreight                     = bcadd((string)$hasFreight, $delivery_money,2);
            }
            
            //运费
            $value['freight']       = $delivery_money;
            
            //计算商品分摊优惠金额明细
            $goodsSingleDiscountMoney               = !empty($value['goodsSingleDiscountMoney'])              ? $value['goodsSingleDiscountMoney']            : '0.00';
            $goodsSingleDiscountRedEnvelopeMoney    = !empty($value['goodsSingleDiscountRedEnvelopeMoney'])   ? $value['goodsSingleDiscountRedEnvelopeMoney'] : '0.00';
            $goodsSingleDiscountActivityMoney       = !empty($value['goodsSingleDiscountActivityMoney'])      ? $value['goodsSingleDiscountActivityMoney']    : '0.00';
            $goodsSingleDiscountCouponMoney         = !empty($value['goodsSingleDiscountCouponMoney'])        ? $value['goodsSingleDiscountCouponMoney']      : '0.00';
            $goodsSingleDiscountPaywayMoney         = !empty($value['goodsSingleDiscountPaywayMoney'])        ? $value['goodsSingleDiscountPaywayMoney']      : '0.00';
            $goodsSingleOrderPrecardBalanceMoney    = !empty($value['goodsSingleOrderPrecardBalanceMoney'])   ? $value['goodsSingleOrderPrecardBalanceMoney'] : '0.00';
            $goodsSingleOrderPoints                 = !empty($value['goodsSingleOrderPoints'])                ? $value['goodsSingleOrderPoints']              : '0.00';
            $goodsSingleDiscountPointMoney          = !empty($value['goodsSingleDiscountPointMoney'])         ? $value['goodsSingleDiscountPointMoney']       : '0.00';
            
            //比较单品优惠汇总是否正确(红包+活动优惠+优惠券+支付方式优惠+预付卡余额支付+积分抵扣金额)
            $single_discount_money                  = bcadd((string)$goodsSingleDiscountRedEnvelopeMoney,(string)$goodsSingleDiscountActivityMoney,2);
            $single_discount_money                  = bcadd((string)$single_discount_money,(string)$goodsSingleDiscountCouponMoney,2);
            $single_discount_money                  = bcadd((string)$single_discount_money,(string)$goodsSingleDiscountPaywayMoney,2);
            $single_discount_money                  = bcadd((string)$single_discount_money,(string)$goodsSingleOrderPrecardBalanceMoney,2);
            $single_discount_money                  = bcadd((string)$single_discount_money,(string)$goodsSingleDiscountPointMoney,2);
            
            //比对单个商品里面优惠明细是否正确
            if( bccomp((string)$single_discount_money,(string)$goodsSingleDiscountMoney,2) != 0 ) {
                return Normal::returnCode(Normal::FAIL,"下单商品编码【{$value['goodsSku']}】优惠明细汇总单价[{$single_discount_money}]不能等于goodsSingleDiscountMoney[{$goodsSingleDiscountMoney}]，汇总单价明细包含[红包+活动优惠+优惠券+支付方式优惠+预付卡余额支付+积分抵扣金额]");
            }
            
            //校验优惠总金额，不能超过销售总价
            if( bccomp((string)$goodsSingleDiscountMoney,(string)$currentGoodsTotalPrice,2) == 1 ) {
                return Normal::returnCode(Normal::FAIL,"下单商品编码【{$value['goodsSku']}】优惠总金额【{$goodsSingleDiscountMoney}】，不能超过销售总价【{$currentGoodsTotalPrice}】");
            }
            
            //校验金额字段不能小于0
            if( bccomp((string)$goodsSingleDiscountMoney,'0.00',2) < 0 ) {
                return Normal::returnCode(Normal::FAIL,"下单商品编码【{$value['goodsSku']}】商品行优惠汇总总金额不能小于0");
            }
            if( bccomp((string)$goodsSingleDiscountPointMoney,'0.00',2) < 0 ) {
                return Normal::returnCode(Normal::FAIL,"下单商品编码【{$value['goodsSku']}】积分抵扣金额不能小于0");
            }
            if( bccomp((string)$goodsSingleDiscountRedEnvelopeMoney,'0.00',2) < 0 ) {
                return Normal::returnCode(Normal::FAIL,"下单商品编码【{$value['goodsSku']}】红包抵扣金额不能小于0");
            }
            if( bccomp((string)$goodsSingleDiscountActivityMoney,'0.00',2) < 0 ) {
                return Normal::returnCode(Normal::FAIL,"下单商品编码【{$value['goodsSku']}】活动优惠金额不能小于0");
            }
            if( bccomp((string)$goodsSingleDiscountCouponMoney,'0.00',2) < 0 ) {
                return Normal::returnCode(Normal::FAIL,"下单商品编码【{$value['goodsSku']}】优惠券抵扣金额不能小于0");
            }
            if( bccomp((string)$goodsSingleDiscountPaywayMoney,'0.00',2) < 0 ) {
                return Normal::returnCode(Normal::FAIL,"下单商品编码【{$value['goodsSku']}】支付方式优惠金额不能小于0");
            }
            if( bccomp((string)$goodsSingleOrderPrecardBalanceMoney,'0.00',2) < 0 ) {
                return Normal::returnCode(Normal::FAIL,"下单商品编码【{$value['goodsSku']}】预付卡抵扣金额不能小于0");
            }
            if( bccomp((string)$goodsSingleOrderPoints,'0.00',2) < 0 ) {
                return Normal::returnCode(Normal::FAIL,"下单商品编码【{$value['goodsSku']}】抵扣积分数量不能小于0");
            }
            
            //优惠金额
            $value['discountMoney']                 = $goodsSingleDiscountMoney;
            $value['discountPointMoney']            = $goodsSingleDiscountPointMoney;
            $value['discountRedEnvelopeMoney']      = $goodsSingleDiscountRedEnvelopeMoney;
            $value['discountActivityMoney']         = $goodsSingleDiscountActivityMoney;
            $value['discountCouponMoney']           = $goodsSingleDiscountCouponMoney;
            $value['discountPaywayMoney']           = $goodsSingleDiscountPaywayMoney;
            $value['discountPrecardMoney']          = $goodsSingleOrderPrecardBalanceMoney;
            $value['discountPoints']                = $goodsSingleOrderPoints;
            
            //汇总所有优惠部分
            $totalDiscountMoney                     = bcadd((string)$totalDiscountMoney,(string)$goodsSingleDiscountMoney,2);
            $totalDiscountActivityMoney             = bcadd((string)$totalDiscountActivityMoney,(string)$goodsSingleDiscountActivityMoney,2);
            $totalDiscountCouponMoney               = bcadd((string)$totalDiscountCouponMoney,(string)$goodsSingleDiscountCouponMoney,2);
            $totalDiscountPointMoney                = bcadd((string)$totalDiscountPointMoney,(string)$goodsSingleDiscountPointMoney,2);
            $totalDiscountPaywayMoney               = bcadd((string)$totalDiscountPaywayMoney,(string)$goodsSingleDiscountPaywayMoney,2);
            $totalDiscountPrecardMoney              = bcadd((string)$totalDiscountPrecardMoney,(string)$goodsSingleOrderPrecardBalanceMoney,2);
            $totalDiscountRedEnvelopeMoney          = bcadd((string)$totalDiscountRedEnvelopeMoney,(string)$goodsSingleDiscountRedEnvelopeMoney,2);
            $totalOrderPoints                       = bcadd((string)$totalOrderPoints,(string)$goodsSingleOrderPoints,2);
            
            //规格数量
            $value['goodsSpecNum']  = checkFields($value['goodsSpecNum']);
            //保质期起始日期
            $startExpDate = isset($value['startExpDate']) ? $value['startExpDate'] : '';
            $value['startExpDate']  = checkFields($startExpDate);
            //保质期结束日期
            $endExpDate = isset($value['endExpDate']) ? $value['endExpDate'] : '';
            $value['endExpDate']    = checkFields($endExpDate);
        }

        //开始核对分摊积分抵扣金额
        if( bccomp($totalDiscountPointMoney, $discountPointMoney,2) !== 0 ) {
            return Normal::returnCode(Normal::FAIL,"订单商品汇总积分抵扣总金额[{$totalDiscountPointMoney}]不等于discountPointMoney[{$discountPointMoney}]");
        }
        
        //开始核对分摊活动抵扣金额
        if( bccomp($totalDiscountActivityMoney, $discountActivityMoney,2) !== 0 ) {
            return Normal::returnCode(Normal::FAIL,"订单商品汇总活动抵扣总金额[{$totalDiscountActivityMoney}]不等于discountActivityMoney[{$discountActivityMoney}]");
        }
        
        //开始核对分摊红包抵扣金额
        if( bccomp($totalDiscountRedEnvelopeMoney, $discountRedEnvelopeMoney,2) !== 0 ) {
            return Normal::returnCode(Normal::FAIL,"订单商品汇总红包抵扣总金额[{$totalDiscountRedEnvelopeMoney}]不等于discountRedEnvelopeMoney[{$discountRedEnvelopeMoney}]");
        }
        
        //开始核对分摊优惠券抵扣金额
        if( bccomp($totalDiscountCouponMoney, $discountCouponMoney,2) !== 0 ) {
            return Normal::returnCode(Normal::FAIL,"订单商品汇总优惠券抵扣总金额[{$totalDiscountCouponMoney}]不等于discountCouponMoney[{$discountCouponMoney}]");
        }
        
        //开始核对分摊预付卡抵扣金额
        if( bccomp($totalDiscountPrecardMoney, $orderPrecardBalanceMoney,2) !== 0 ) {
            return Normal::returnCode(Normal::FAIL,"订单商品汇总预付卡抵扣总金额[{$totalDiscountPrecardMoney}]不等于discountPrecardMoney[{$orderPrecardBalanceMoney}]");
        }
        
        //开始核对分摊支付方式优惠抵扣金额
        if( bccomp($totalDiscountPaywayMoney, $discountPaywayMoney,2) !== 0 ) {
            return Normal::returnCode(Normal::FAIL,"订单商品汇总支付方式优惠总金额[{$totalDiscountPaywayMoney}]不等于discountPaywayMoney[{$discountPaywayMoney}]");
        }
        
        //开始核对分摊优惠总抵扣金额
        if( bccomp($totalDiscountMoney, $discountMoney,2) !== 0 ) {
            return Normal::returnCode(Normal::FAIL,"订单商品汇总优惠总抵扣金额[{$totalDiscountMoney}]不等于discountMoney[{$discountMoney}]");
        }
        
        //开始核对分摊总积分
        if( bccomp($totalOrderPoints, $orderPoints,2) !== 0 ) {
            return Normal::returnCode(Normal::FAIL,"订单商品汇总积分[{$totalOrderPoints}]不等于orderPoints[{$orderPoints}]");
        }
        
        //汇总推送优惠明细
        $sum_discount_money = bcadd($discountPointMoney,$discountRedEnvelopeMoney,2);
        $sum_discount_money = bcadd($sum_discount_money,$discountActivityMoney,2);
        $sum_discount_money = bcadd($sum_discount_money,$discountCouponMoney,2);
        $sum_discount_money = bcadd($sum_discount_money,$discountPaywayMoney,2);
        $sum_discount_money = bcadd($sum_discount_money,$orderPrecardBalanceMoney,2);
        
        //处理特殊字段
        $param['orderRemarks']      = $param['orderRemarks'] ?? '';
        $param['freight']           = $param['freight'] ?? 0.00;
        $param['freightTax']        = $param['freightTax'] ?? 0.00;
        $param['discountMoney']     = $param['discountMoney'] ?? 0.00;
        $param['discountRedEnvelopeName'] = $param['discountRedEnvelopeName'] ?? '';
        $param['discountRedEnvelopeSaleMoney'] = $param['discountRedEnvelopeSaleMoney'] ?? '0.00';
        
        //校验订单金额是否正确
        $freight            = $param['freight'] ?? 0.00;
        $freightTax         = $param['freightTax'] ?? 0.00;
        //所有运费
        $checkTotalMoney    = bcadd($freight, $freightTax,2);
        //商品金额
        $checkTotalMoney    = bcadd($goodsTotalPrice, $checkTotalMoney,2);
        //订单总金额 = 所有运费 + 商品金额 - 优惠金额 
        $checkTotalMoney    = bcsub($checkTotalMoney,$discountMoney,2);
        
        //实际接口提交订单金额
        $orderMoney = $param['orderMoney'];
        if( bccomp($orderMoney,'0.00',2) < 0 ) {
            return Normal::returnCode(Normal::FAIL,"订单实际支付金额orderMoney不能小于0");
        }
        
        if( bccomp($orderMoney, $checkTotalMoney,2) !== 0 ) {
            return Normal::returnCode(Normal::FAIL,"订单实际支付金额orderMoney（{$orderMoney}）计算错误，订单金额计算公式【订单实际支付金额orderMoney（{$checkTotalMoney}）= 运费freight（{$freight}）+ 抵扣前商品行金额总和 sum(goodsNum*goodsPrice)（{$goodsTotalPrice}） - 非现金抵扣金额 discountMoney （{$discountMoney}）】");
        }
        
        //下单类型字段
        if( isset($param['orderType']) && !empty($param['orderType']) ) {
            $param['order_type'] = $param['orderType'];
            unset($param['orderType']);
        }
        
        $input['requestData'] = $param;
        return Normal::returnCode(Normal::SUC,"验证通过",$input);
    }
}